package com.bigkoo.pickerviewdemo.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Fcy on 2016/6/19.
 */
public class DateUtil {

  public static SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy/MM/dd");
  public static final long ONE_DAY = 1000 * 60 * 60 * 24;

  public static String getTimeStr(long time) {
    return mDateFormat.format(new Date(time * 1000));
  }

  // 获得当前日期与本周一相差的天数
  public static int getMondayPlus() {
    Calendar cd = Calendar.getInstance();
    // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
    int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
    if (dayOfWeek == 1) {
      return -6;
    } else {
      return 2 - dayOfWeek;
    }
  }

  /**
   * @return 获取当天0点的时间戳 单位 s
   */
  public static long getCurrDayTime() {
    //return System.currentTimeMillis() / 1000;
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis() / 1000;
  }

  /**
   * 将cal指定的时间改为当天的0点
   */
  public static void handleDayStart(Calendar cal) {
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
  }

  /**
   * @return 本周周一的时间戳 s
   */
  public static long getCurrMondayTime() {
    int mondayPlus = getMondayPlus();
    GregorianCalendar gc = new GregorianCalendar();
    gc.add(GregorianCalendar.DATE, mondayPlus);
    handleDayStart(gc);
    return getTimeFromCalendar(gc);
    //        return mDateFormat.format(currentDate.getTime());
  }

  public static long getTimeFromCalendar(Calendar calendar) {
    return calendar.getTimeInMillis() / 1000;
  }

  /**
   * @return 本周周末的时间戳 s
   */
  public static long getCurrSundayTime() {
    int mondayPlus = getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
    return getTimeFromCalendar(currentDate);
  }

  // 获得当前月--开始日期 0点
  public static long getMinMonthTime() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
    handleDayStart(calendar);
    return getTimeFromCalendar(calendar);
  }

  // 获得当前月--结束日期
  public static long getMaxMonthTime() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    return getTimeFromCalendar(calendar);
  }

  /**
   * 获取指定时间的下一天
   */
  public static Calendar getNextDay(long timeStamp) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timeStamp);
    calendar.add(Calendar.DATE, 1);
    return calendar;
  }

  /**
   * 指定时间戳是否是今天
   */
  public static boolean isToday(long timeStamp1) {
    return mDateFormat.format(new Date(timeStamp1)).equals(mDateFormat.format(new Date()));
  }

  /**
   * 两个指定时间戳是否是同一天
   */
  public static boolean isSameDay(long timeStamp1, long timeStamp2) {
    return mDateFormat.format(new Date(timeStamp1))
        .equals(mDateFormat.format(new Date(timeStamp1)));
  }

  /**
   * 两个时间戳相差的时间天数，边界算一天
   */
  public static int getDayOffset(long timeSpa, long timeSpa2) {
    return (int) ((timeSpa2 - timeSpa) / ONE_DAY + 1);
  }
}
